package mypackage.dao.impl;

import mypackage.bean.Admin;
import mypackage.dao.AdminDao;
import mypackage.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author YH925
 * @create 2021/12/12 21:08
 */
public class AdminDaoImpl implements AdminDao {
    Connection connection;

    {
        try {
            connection = JDBCUtils.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    QueryRunner runner = new QueryRunner();

    /**
     * 管理员登录方法
     * @param username
     * @param password
     * @return
     */
    @Override
    public Admin adminLogin(String username, String password) {
        String sql = "select * from saleAdmin where name = ? and password = ?";
        BeanHandler<Admin>handler = new BeanHandler<>(Admin.class);
        Admin admin = null;
        try {
            admin = runner.query(connection, sql, handler, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }
}
